<template>
  <div class="top-category">
    <div class="container">
      <!-- 面包屑 -->
      <bread>
        <bread-item to="/">首页</bread-item>
        <transition name="fade-right" mode="out-in">
          <!-- 添加key，让diff算法刷新节点 -->
          <bread-item v-if="topCategory" :key="topCategory.id">{{
            topCategory.name
          }}</bread-item>
        </transition>
      </bread>
      <!-- 轮播图 -->
      <carousel :slides="sliders" style="height: 500px" />
      <!-- 所有二级分类 -->
      <div class="sub-list" v-if="topCategory">
        <h3>全部分类</h3>
        <ul>
          <li v-for="sub in topCategory.children" :key="sub.id">
            <a href="javascript:;">
              <img :src="sub.picture" />
              <p>{{ sub.name }}</p>
            </a>
          </li>
        </ul>
      </div>
      <!-- 不同分类商品 -->
      <div class="ref-goods" v-for="item in subList" :key="item.id">
        <div class="head">
          <h3>- {{ item.name }} -</h3>
          <p class="tag">{{ item.desc }}</p>
          <More :to="`/category/sub/${item.id}`" />
        </div>
        <div class="body">
          <category-item v-for="i in item.goods" :key="i.id" :goods="i" />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
  import {
    findBanner,
    findTopCategory
  } from "api/home.js";
  import {
    ref,
    computed,
    watch
  } from "vue";
  import {
    useStore
  } from "vuex";
  import {
    useRoute
  } from "vue-router";
  import CategoryItem from "./components/CategoryItem.vue";
  export default {
    components: {
      CategoryItem
    },
    name: "TopCategory",
    setup() {
      // 轮播图
      const sliders = ref([]);
      findBanner().then((data) => {
        sliders.value = data.result;
      });
      //全部分类
      const store = useStore();
      const route = useRoute();
      const topCategory = computed(() => {
        return store.state.catagory.catagory.find(
          (item) => item.id === route.params.id
        );
      });
      //推荐商品
      const subList = ref([]);
      const getSubList = () => {
        findTopCategory(route.params.id).then((data) => {
          subList.value = data.result.children;
        });
      };

      //监听路由变化，刷新值，否则根据路由规则，不会刷新setup
      watch(
        () => route.params.id,
        (newValue) => {
          //首先有值，然后必须是顶级类目的地址才跳转，sub不跳转，防止多发请求返回400
          newValue && `/category/${newValue}` === route.path && getSubList();
        }, {
          immediate: true,
        }
      );
      return {
        sliders,
        topCategory,
        subList
      };
    },
  };

</script>
<style scoped lang="less">
  .top-category {
    h3 {
      font-size: 28px;
      color: #666;
      font-weight: normal;
      text-align: center;
      line-height: 100px;
    }

    .sub-list {
      margin-top: 20px;
      background-color: #fff;

      ul {
        display: flex;
        padding: 0 32px;
        flex-wrap: wrap;

        li {
          width: 168px;
          height: 160px;

          a {
            text-align: center;
            display: block;
            font-size: 16px;

            img {
              width: 100px;
              height: 100px;
            }

            p {
              line-height: 40px;
            }

            &:hover {
              color: @xtxColor;
            }
          }
        }
      }
    }
  }

  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;

    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }

      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }

    .body {
      display: flex;
      justify-content: flex-start;
      flex-wrap: wrap;
      padding: 0 65px 30px;
    }
  }

</style>
